﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ZLDC.Common.Estate;

namespace ZLDC.DAL.Estate
{
                public class ChangecustomerLogDAL
    {
                                                                                public static DataTable p_changecustomerlog_list_table(int page, int pagesize, string condition, ref int total, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@page", page));
            parms.Add(new SqlParameter("@pagesize", pagesize));
            parms.Add(new SqlParameter("@condition", condition));

            SqlParameter pTotal = new SqlParameter("@total", SqlDbType.Int, 4);
            pTotal.Direction = ParameterDirection.InputOutput;
            pTotal.Value = total;
            parms.Add(pTotal);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_changecustomerlog_list", parms);

            total = (pTotal.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pTotal.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

                                                                public static void p_changecustomerlog_check(int id, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@id", id));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_changecustomerlog_check", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }

                                                                        public static void p_changecustomerlog_change(int typeid, int id, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@typeid", typeid));
            parms.Add(new SqlParameter("@id", id));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.execute_procedure("p_changecustomerlog_change", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

        }

                                                                                                                                                public static void p_changeCustomer_log_save(int ID, int CId, int changeDeptId, string changeDept, int changeUserId, string changeUser, int acceptDeptId, string acceptDept, int acceptUserId, string acceptUser, int status, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@ID", ID));
            parms.Add(new SqlParameter("@CId", CId));
            parms.Add(new SqlParameter("@changeDeptId", changeDeptId));
            parms.Add(new SqlParameter("@changeDept", changeDept));
            parms.Add(new SqlParameter("@changeUserId", changeUserId));
            parms.Add(new SqlParameter("@changeUser", changeUser));
            parms.Add(new SqlParameter("@acceptDeptId", acceptDeptId));
            parms.Add(new SqlParameter("@acceptDept", acceptDept));
            parms.Add(new SqlParameter("@acceptUserId", acceptUserId));
            parms.Add(new SqlParameter("@acceptUser", acceptUser));
            parms.Add(new SqlParameter("@status", status));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_changeCustomer_log_save", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }

                public static int UpdateCustomerCheckList(int isself, int mark1, int id, _Database db)
        {
            int rows = 0;
            try
            {
                string sql = "update t_customer set isSelf=" + isself + ",ischange=0, mark1='" + mark1 + "',ownerdate='" + DateTime.Now.ToString() + "' where Id= " + id + " ";

                if (isself == 0) 
                {
                    sql = "update t_customer set isSelf=" + isself + ",status=0,ischange=0, mark1='" + mark1 + "',ownerdate='" + DateTime.Now.ToString() + "' where Id= " + id + " ";
                }

                rows = db.ExecuteSql(sql);
            }
            catch { }
            finally { db.close(); }

            return rows;
        }

                                                                        public static int UpdateCustomerCheckList(int isself, int mark1, int id, int ownerid, string owner, int ownerdeptid, string ownerdept, _Database db)
        {
            int rows = 0;
            try
            {
                string sql = "update t_customer set isSelf=" + isself + ",ischange=0,status=0,mark1='" + mark1 + "', ownerid=" + ownerid + ",owner='" + owner + "', ownerdeptid=" + ownerdeptid + ",ownerdept='" + ownerdept + "', ownerdate='" + DateTime.Now.ToString() + "' where Id= " + id + " ";
                rows = db.ExecuteSql(sql);
            }
            catch { }
            finally { db.close(); }

            return rows;
        }

                                                                public static int p_customer_ischange_public(int sessionUserId, int customerId, out string outmsg, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@sessionUserId", sessionUserId));
            parms.Add(new SqlParameter("@customerId", customerId));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.execute_procedure("p_customer_ischange_public", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return retval;
        }


                                                        public static DataTable p_CustomerChangelog_ChangeUserId(int id,_Database db)
        {
            DataSet ds = null;
            DataTable dt = null;
            try
            {
                string sql = "select changeUserId,status From t_changeCustomer_log where CId=" + id + " order by Id asc ";
                ds = db.Query(sql);

                if (ds != null && ds.Tables.Count > 0)
                    dt = ds.Tables[0];
            }
            catch { }
            finally { db.close(); }

            return dt;
        }


    }
}
